package com.shop.prj.dao.Impl;

import com.shop.prj.bean.Book;
import com.shop.prj.bean.BookStyle;
import com.shop.prj.dao.BookStyleDao;
import com.shop.prj.utils.JDBCUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class BookStyleDaoImpl implements BookStyleDao {
    @Override
    public int insert(BookStyle bookStyle) {
        String sql="INSERT INTO `bookstyle`(`bookStyle_type`,`bookStyle_create_time`) VALUES (?,?)";
        int result=0;
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        connection= JDBCUtils.getConnection();
        try {
            preparedStatement=connection.prepareStatement(sql);
//            preparedStatement.setInt(1, bookStyle.getBookStyleId());
            preparedStatement.setString(1, bookStyle.getBookStyleType());
            preparedStatement.setDate(2,bookStyle.getBookStyleCreateTime());
            result=preparedStatement.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                preparedStatement.close();
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();

            }
        }
        return result;
    }

    @Override
    public int update(BookStyle bookStyle) {

        String sql="UPDATE `BookStyle` SET `bookstyle_type`=?,`bookStyle_create_time`=? WHERE  `bookStyle_id`=?";
        int result=0;
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        connection= JDBCUtils.getConnection();
        try {
            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setInt(3, bookStyle.getBookStyleId());
            preparedStatement.setString(1, bookStyle.getBookStyleType());
            preparedStatement.setDate(2,bookStyle.getBookStyleCreateTime());
            result=preparedStatement.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                preparedStatement.close();
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();

            }
        }
        return result;
    }

    @Override
    public int delete(Long bookStyleId) {
        String sql = "DELETE FROM `BookStyle` WHERE `bookStyle_Id` = ?";
        Connection connection = JDBCUtils.getConnection();
        PreparedStatement preparedStatement = null;
        int result = 0;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setLong(1, bookStyleId);
            result = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                preparedStatement.close();
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

   

    @Override
    public List<BookStyle> queryAll() {
        String sql = "SELECT * FROM `bookStyle`";

        Connection connection = JDBCUtils.getConnection();
        Statement statement = null;
        ResultSet resultSet = null;
        BookStyle bookStyle=null;
        List<BookStyle> list = new ArrayList<BookStyle>();
        try {
            statement = connection.createStatement();
            resultSet = statement.executeQuery(sql);
            while (resultSet.next()) {

                 bookStyle = new BookStyle();
                bookStyle.setBookStyleId(resultSet.getInt(1));
                bookStyle.setBookStyleType(resultSet.getString(2));
                bookStyle.setBookStyleCreateTime(resultSet.getDate(3));
                list.add(bookStyle);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                resultSet.close();
                statement.close();
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    @Override
	public BookStyle queryByBookStyleName(String bookStyleName) {
    	String sql = "SELECT * FROM `bookStyle` WHERE `bookStyle_type`=?";

        Connection connection = JDBCUtils.getConnection();
        PreparedStatement preparedStatement = null;

        ResultSet resultSet = null;
        BookStyle  bookStyle = null;

        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, bookStyleName);
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                bookStyle = new BookStyle();
                bookStyle.setBookStyleId(resultSet.getInt(1));
                bookStyle.setBookStyleType(resultSet.getString(2));
                bookStyle.setBookStyleCreateTime(resultSet.getDate(3));




            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                resultSet.close();
                preparedStatement.close();
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }


        return bookStyle;
    }

	@Override
	public BookStyle queryByBookStyleId(Integer bookStyleId) {
		// TODO Auto-generated method stub
		 String sql = "SELECT * FROM `bookStyle` WHERE `bookStyle_Id`=?";

	        Connection connection = JDBCUtils.getConnection();
	        PreparedStatement preparedStatement = null;

	        ResultSet resultSet = null;
	        BookStyle  bookStyle = null;

	        try {
	            preparedStatement = connection.prepareStatement(sql);
	            preparedStatement.setInt(1, bookStyleId);
	            resultSet = preparedStatement.executeQuery();
	            if (resultSet.next()) {
	                bookStyle = new BookStyle();
	                bookStyle.setBookStyleId(resultSet.getInt(1));
	                bookStyle.setBookStyleType(resultSet.getString(2));
	                bookStyle.setBookStyleCreateTime(resultSet.getDate(3));

	            }
	        } catch (SQLException e) {
	            e.printStackTrace();
	        } finally {
	            try {
	                resultSet.close();
	                preparedStatement.close();
	                connection.close();
	            } catch (SQLException e) {
	                e.printStackTrace();
	            }

	        }


	        return bookStyle;
	
	}

	@Override
	public List<BookStyle> queryByBookStyle(String bookStyleName) {
		// TODO Auto-generated method stub
		 String sql = "SELECT * FROM `BookStyle`WHERE `bookStyle_type`LIKE ?";


	        Connection connection = JDBCUtils.getConnection();
	        PreparedStatement preparedStatement = null;
	        ResultSet resultSet = null;
	        
	        List<BookStyle> list = new ArrayList<BookStyle>();
	        try {
	            preparedStatement = connection.prepareStatement(sql);
	            preparedStatement.setString(1, "%" + bookStyleName + "%");

	            resultSet = preparedStatement.executeQuery();
	            while (resultSet.next()) {
	            	BookStyle bookStyle=new BookStyle();
	            	bookStyle.setBookStyleId(resultSet.getInt(1));
	                bookStyle.setBookStyleType(resultSet.getString(2));
	                bookStyle.setBookStyleCreateTime(resultSet.getDate(3));
	           
	                list.add(bookStyle);
	            }
	        } catch (SQLException e) {
	            e.printStackTrace();
	        } finally {
	            try {
	                resultSet.close();
	                preparedStatement.close();
	                connection.close();
	            } catch (SQLException e) {
	                e.printStackTrace();
	            }
	        }
	        return list;
	}

	
}
